package xhwl.insurance.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.RectF;
import android.os.Bundle;
import android.text.Layout;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.umeng.socialize.utils.Log;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;
import uk.co.senab.photoview.PhotoViewAttacher.OnMatrixChangedListener;
import uk.co.senab.photoview.PhotoViewAttacher.OnPhotoTapListener;
import xhwl.android.common.log.XHLog;
import xhwl.insurance.BaseActivity;
import xhwl.insurance.BuildConfig;
import xhwl.insurance.R;
import xhwl.insurance.dialog.ImageLoadingDialog;

public class ImageShowerActivity extends BaseActivity implements View.OnClickListener{
    static final String PHOTO_TAP_TOAST_STRING = "Photo Tap! X: %.2f %% Y:%.2f %% ID: %d";
    static final String SCALE_TOAST_STRING = "Scaled to: %.2ff";
    static final String FLING_LOG_STRING = "Fling velocityX: %.2f, velocityY: %.2f";
    public static final String EXTRA_IMAGE_URL = "imgshow";
    private PhotoViewAttacher mAttacher;

    private Toast mCurrentToast;

    private Intent intent;
    private String imageType;
    private String imageType1;
    private PhotoView photoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏 
        setContentView(R.layout.activity_image_shower);


        intent = getIntent();
        photoView = (PhotoView) findViewById(R.id.iv_photo);
        final ImageLoadingDialog dialog = new ImageLoadingDialog(this);
        dialog.setCanceledOnTouchOutside(false);
        String imagePath = intent.getStringExtra(EXTRA_IMAGE_URL);
        imageType1 = intent.getStringExtra("type");

        if (!imagePath.startsWith("http")) {
            imagePath = "file://" + imagePath;
        }
        ImageLoader loader = ImageLoader.getInstance();
        loader.displayImage(imagePath, photoView,
                new ImageLoadingListener() {

                    @Override
                    public void onLoadingStarted(String arg0, View arg1) {
                        // TODO Auto-generated method stub
                        dialog.show();
                    }

                    @Override
                    public void onLoadingFailed(String arg0, View arg1,
                                                FailReason arg2) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }

                    @Override
                    public void onLoadingComplete(String arg0, View arg1,
                                                  Bitmap arg2) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }

                    @Override
                    public void onLoadingCancelled(String arg0, View arg1) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                });
        mAttacher = new PhotoViewAttacher(photoView);

        // Lets attach some listeners, not required though!
        mAttacher.setOnMatrixChangeListener(new MatrixChangeListener());
        mAttacher.setOnPhotoTapListener(new PhotoTapListener());
        mAttacher.setOnSingleFlingListener(new SingleFlingListener());


        initView();
    }

    @Override
    public void initViews() {
        initTitleViews();
    }

    @Override
    public void initListener() {
//        photoView.setOnClickListener(this);

    }

    @Override
    public void loadData() {

    }

    private void initView() {
        TextView mTitle = (TextView) findViewById(R.id.title_textview);
        LinearLayout mLLTitle=(LinearLayout)findViewById(R.id.ll_title);



        switch (imageType1){
            case "1":
                mTitle.setText("身份证正面");
                break;
            case "2":
                mTitle.setText("身份证反面");
                break;
            case "3":
                mTitle.setText("HMO卡正面");
                break;
            case "4":
//                mTitle.setText("放大图片");
                mLLTitle.setVisibility(View.GONE);
                break;
            case "5":
                mTitle.setText("人工客服");
                break;
            default:
                break;

        }
        LinearLayout llBack = (LinearLayout) findViewById(R.id.llBack);
        llBack.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                onBackPressed();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_photo:
                Toast.makeText(mContext, "点击了图片", Toast.LENGTH_SHORT).show();
                finish();
                break;
            default:
                break;
        }

    }

    private class MatrixChangeListener implements OnMatrixChangedListener {

        @Override
        public void onMatrixChanged(RectF rect) {

        }
    }

    private class SingleFlingListener implements
            PhotoViewAttacher.OnSingleFlingListener {

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                               float velocityY) {
            if (BuildConfig.DEBUG) {
                XHLog.d(String.format(FLING_LOG_STRING, velocityX, velocityY));
            }
            return true;
        }
    }

    private class PhotoTapListener implements OnPhotoTapListener {

        @Override
        public void onPhotoTap(View view, float x, float y) {
            float xPercentage = x * 100f;
            float yPercentage = y * 100f;

            showToast(String.format(PHOTO_TAP_TOAST_STRING, xPercentage,
                    yPercentage, view == null ? 0 : view.getId()));
        }

        private void showToast(CharSequence text) {
            if (null != mCurrentToast) {
                mCurrentToast.cancel();
            }
        }

        public boolean onTouchEvent(MotionEvent event) {
            // TODO Auto-generated method stub
            Toast.makeText(mContext, "点击了======消失", Toast.LENGTH_SHORT).show();
            finish();

            return true;
        }

        @Override
        public void onOutsidePhotoTap() {
            // TODO Auto-generated method stub
            finish();
        }

    }

}
